package com.binarysoft.service.common.ping;

import java.net.InetAddress;
import java.util.Scanner;

import common.framework.dsb.client.DefaultServiceFactory;
import common.framework.dsb.client.ServiceFactory;

public class TestPing {

	public static void main(String[] args) throws Exception {

		//
		Process process = Runtime.getRuntime().exec("cmd.exe /c ping www.baidu.com");
		Scanner scanner = new Scanner(process.getInputStream(), "GB2312");
		while (scanner.hasNext()) {
			String line = scanner.nextLine();
			System.out.println(line);
			// 解析line(此处省略)
		}

		//
		int timeout = 1000;
		InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
		boolean reachable = inetAddress.isReachable(timeout);

		ServiceFactory serviceFactory = new DefaultServiceFactory();
		try {
			PingService pingService = serviceFactory.lookup("DSB/PingService", "localhost", 10001);
			inetAddress = InetAddress.getByName("www.baidu.com");
			inetAddress.isReachable(timeout);

			PingResult pingResult = pingService.doPing(inetAddress);
			System.out.println(pingResult.isTimedout() + " " + pingResult.getTtl() + " " + pingResult.getRtt());

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

class PingControlThread extends Thread {
	PingService pingService = null;
	String ip;

	public PingControlThread(PingService pingService, String ip) {
		this.pingService = pingService;
		this.ip = ip;
	}

	public void run() {
		try {
			for (int i = 1; i < 254; i++) {
				new PingThread(pingService, "132.159.156." + i).start();

				// PingResult result =
				// pingService.doPing(InetAddress.getByName(ip));
				// System.out.println(ip+" - "+result.isTimedout()+" "+result.getRtt()+" "+result.getTtl());
			}
		} catch (Exception e) {
			e.printStackTrace(System.err);
		}
	}

}

class PingThread extends Thread {
	PingService pingService = null;
	String ip;

	public PingThread(PingService pingService, String ip) {
		this.pingService = pingService;
		this.ip = ip;
	}

	public void run() {
		try {
			PingResult result = pingService.doPing(InetAddress.getByName(ip));
			System.out.println(this.ip + " - " + result.isTimedout() + " " + result.getRtt() + " " + result.getTtl());
		} catch (Exception e) {
			e.printStackTrace(System.err);
		}
	}
}
